import {Sprite, loader} from '../LayaLib.js'

export const WIDTH = 64

export class Block extends Sprite {
    constructor(type) {
        super()
        this.y = 360
        this.update(type)
    }
}

var _proto = Block.prototype

_proto.update = function(type) {
    this.type = type
    this.graphics.clear()
    var h = type * 50
    if(h) {
        // var c = '#'+(~~('#FFFFFF'.replace('#', '0x')*Math.random())).toString(16) //随机颜色
        this.graphics.drawRect(0, 0, WIDTH, h, '#222222')
    }
    this.pivotY = h
}

/**
 * 角色碰撞测试
 * @param {Role} role 角色
 * @param {Number} dx 当前障碍的左边框与视窗的距离
 */
_proto.test = function(role, dx) {
    var f = 360 - this.pivotY
    if(role.y <= f) {
        if(role.floor !== f) {
            if(role.floor < f) {
                //前一个障碍比较低，往下跳
                if(dx < role.x - role.WIDTH) {
                    role.skip(0)
                    role.floor = f
                    // role.distance++
                }
            }else {
                //前一个障碍比较高
                if(dx < role.x - role.WIDTH * 0.5) {
                    role.floor = f
                    // role.distance++
                }
            }
        }
        //如果已经被推到后面就往前走
        if(role.x < role.limitX) role.x++
    }else {
        //被往后推
        role.x = dx
    }
}